package com.woniu.controller;

import com.woniu.entity.Dept;
import com.woniu.entity.PageInfo;
import com.woniu.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping("dept")
@Controller
public class DeptController {
    @Resource
    private DeptService deptService;
    @GetMapping("list")
    public List<Dept> getAllDepts(){
       // deptService.getAllDepts()
        return deptService.getAllDepts();
    }

    @GetMapping("listType")
    @ResponseBody
    public List<Dept>  getAllDept(){
//        model.addAttribute("deptList",deptService.getAllDepts());
//        return "update";
        System.out.println( deptService.getAllDepts());
        return deptService.getAllDepts();
    }

    @GetMapping("listAjax")
    @ResponseBody
    public Map<String,Object> listAjax(PageInfo pageInfo){

        Map<String,Object> resultMap = new HashMap<>();
        try {
            List<Dept> deptList = deptService.listByPage(pageInfo);
            resultMap.put("deptList",deptList);
            resultMap.put("pageInfo",pageInfo);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    @PostMapping("save")
    public String save(Dept dept){
        deptService.addDept(dept);
        return "redirect:Deptlist";
    }

    @PostMapping("deleteSelect")
    public String deleteSelect(@RequestParam List<Integer> deptnoes){
        deptService.deleteSelect(deptnoes);
        return "redirect:DeptList";
    }
}

